﻿using System;
using System.Collections;
using System.Collections.Generic;

using UnityEngine;

public class MaxInterstitialUnit : MonoBehaviour {

	private string interstitialAdUnitId;

	private string alias;
	public float retryTime = 3f;
	public Action<string, string, int> callback;
	// Use this for initialization
	public void Init(string adUnitId, string name)
	{
        
		interstitialAdUnitId = adUnitId;
		alias = name;

		if (string.IsNullOrEmpty(adUnitId))
		{
			Debug.LogError(alias + " 插屏广告UnitId为空");
			return;
		}

#if UNITY_IOS || UNITY_ANDROID || UNITY_IPHONE 
		if (MaxSdk.IsInitialized())
		{
			InitializeInterstitialAds();
		}
		else
		{
			MaxSdkCallbacks.OnSdkInitializedEvent += Initialize;
		}
#endif
	}
	
	public bool ShowAds()
	{
#if UNITY_IOS || UNITY_ANDROID || UNITY_IPHONE 
		if (MaxSdk.IsInterstitialReady(interstitialAdUnitId))
		{
			MaxSdk.ShowInterstitial(interstitialAdUnitId);
			return true;
		}
		else
		{
			return false;
		}
#else
		return false;
#endif
	}

	public bool IsReady()
	{
#if UNITY_IOS || UNITY_ANDROID || UNITY_IPHONE 
		return MaxSdk.IsInterstitialReady(interstitialAdUnitId);
#else
		return true;
#endif
	}

	private void OnDestroy()
	{
#if UNITY_IOS || UNITY_ANDROID || UNITY_IPHONE 
		MaxSdkCallbacks.OnSdkInitializedEvent -= Initialize;
		MaxSdkCallbacks.OnInterstitialLoadedEvent -= OnInterstitialLoadedEvent;
		MaxSdkCallbacks.OnInterstitialLoadFailedEvent -= OnInterstitialFailedEvent;
		MaxSdkCallbacks.OnInterstitialAdFailedToDisplayEvent -= InterstitialFailedToDisplayEvent;
		MaxSdkCallbacks.OnInterstitialHiddenEvent -= OnInterstitialDismissedEvent;
#endif
	}

	public void InitializeInterstitialAds()
	{
#if UNITY_IOS || UNITY_ANDROID || UNITY_IPHONE 
		// Attach callback
		MaxSdkCallbacks.OnInterstitialLoadedEvent += OnInterstitialLoadedEvent;
		MaxSdkCallbacks.OnInterstitialLoadFailedEvent += OnInterstitialFailedEvent;
		MaxSdkCallbacks.OnInterstitialAdFailedToDisplayEvent += InterstitialFailedToDisplayEvent;
		MaxSdkCallbacks.OnInterstitialHiddenEvent += OnInterstitialDismissedEvent;
#endif

		// Load the first interstitial
		LoadInterstitial();
	}
	
	

	void Initialize(MaxSdkBase.SdkConfiguration sdkConfiguration)
	{
		InitializeInterstitialAds();
	}

	private void LoadInterstitial()
	{
#if UNITY_IOS || UNITY_ANDROID || UNITY_IPHONE 
		MaxSdk.LoadInterstitial( interstitialAdUnitId );
#endif
	}

	private void OnInterstitialLoadedEvent(string adUnitId)
	{
		if (adUnitId != interstitialAdUnitId) return;
		// Rewarded ad is ready to be shown. MaxSdk.IsRewardedAdReady(rewardedAdUnitId) will now return 'true'
		if (callback != null)
		{
			callback("OnInterstitialLoadedEvent", adUnitId, 0);
		}
		// Interstitial ad is ready to be shown. MaxSdk.IsInterstitialReady(interstitialAdUnitId) will now return 'true'
	}

	private void OnInterstitialFailedEvent(string adUnitId, int errorCode)
	{
		if (adUnitId != interstitialAdUnitId) return;
		// Rewarded ad is ready to be shown. MaxSdk.IsRewardedAdReady(rewardedAdUnitId) will now return 'true'
		if (callback != null)
		{
			callback("OnInterstitialFailedEvent", adUnitId, errorCode);
		}
		// Interstitial ad failed to load. We recommend re-trying in 3 seconds.
		Invoke("LoadInterstitial", 3);
	}

	private void InterstitialFailedToDisplayEvent(string adUnitId, int errorCode)
	{
		if (adUnitId != interstitialAdUnitId) return;
		// Rewarded ad is ready to be shown. MaxSdk.IsRewardedAdReady(rewardedAdUnitId) will now return 'true'
		if (callback != null)
		{
			callback("InterstitialFailedToDisplayEvent", adUnitId, errorCode);
		}
		// Interstitial ad failed to display. We recommend loading the next ad
		LoadInterstitial();
	}

	private void OnInterstitialDismissedEvent(string adUnitId)
	{
		if (adUnitId != interstitialAdUnitId) return;
		// Rewarded ad is ready to be shown. MaxSdk.IsRewardedAdReady(rewardedAdUnitId) will now return 'true'
		if (callback != null)
		{
			callback("OnInterstitialDismissedEvent", adUnitId, 0);
		}
		// Interstitial ad is hidden. Pre-load the next ad
		LoadInterstitial();
	}

}
